package com.mp.approvalreq.servlet.log;

import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.plusmoney.apollo.ApolloEntrypoint;
import com.plusmoney.apollo.ConfigFileWatchDog;
import com.plusmoney.apollo.GeneralWatchDog;
import org.apache.log4j.PropertyConfigurator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import java.util.ArrayList;
import java.util.List;

public class Log4jInit extends HttpServlet {

    private static final long serialVersionUID = -8070522100072491814L;

    public Log4jInit() {
        super();
    }

    @Override
    public void init() throws ServletException {
        try {
            ApolloEntrypoint.register(buildConfigFile());
        } catch (Exception e) {
            e.printStackTrace();
        }
        String log4jDec = Log4jInit.class.getResource("/").getPath();
        String file = log4jDec + "conf-log/log4j.properties";
        PropertyConfigurator.configure(file);
    }


    private static List<ConfigFileWatchDog> buildConfigFile() {
        List<ConfigFileWatchDog> list = new ArrayList<>();
        list.add(buildWatchDog("log4j", "conf-log/log4j.properties", ConfigFileFormat.Properties));
        list.add(buildWatchDog("proxy", "proxy.properties", ConfigFileFormat.Properties));
        list.add(buildWatchDog("client", "client.cnf", ConfigFileFormat.TXT));
        list.add(buildWatchDog("rocketMq", "rocketmq.properties", ConfigFileFormat.Properties));
        list.add(buildWatchDog("redis", "redis.properties", ConfigFileFormat.Properties));
        return list;
    }

    private static GeneralWatchDog buildWatchDog(String namespace, String localFile, ConfigFileFormat fileFormat) {
        return new GeneralWatchDog(namespace, Log4jInit.class.getClassLoader().getResource(localFile).getPath(), fileFormat);
    }

    @Override
    public void destroy() {
        super.destroy();
    }
}
